﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SysCommon.Tools;

namespace CVSS.Client.CIQS.UI
{
    public class VehicleStatByCarmemberFormMDT
    {
        VehicleStatByCarmemberForm frm = new VehicleStatByCarmemberForm();
        //string m_CurrentOrgName = "";

        #region 界面初始化ShowForm()
        /// <summary>
        /// 界面初始化
        /// </summary>
        /// <returns></returns>
        public bool ShowForm()
        {
            try
            {
                frm.Load+=new EventHandler(frm_Load);
                frm.ShowDialog();
                return true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用CVSS.Client.CIQS.UI.VehicleStatByCarmemberFormMDT出错", ex);
            }
            return false;
        }
        #endregion

        void frm_Load(object sender, EventArgs e)
        {

            SysCommon.MsgBox.ProecssingMessageBox.StartProecssing(this.frm, "正在加载窗体，请稍候...");
            System.Windows.Forms.Application.DoEvents();
            frm.btn_ExportToExcel.Click += new EventHandler(btn_ExportToExcel_Click);
            frm.btn_Print.Click += new EventHandler(btn_Print_Click);
            frm.btn_Start.Click += new EventHandler(btn_Start_Click);
            //frm.btn_ExportToExcel.Enabled = false;
            //frm.btn_Print.Enabled = false;
            frm.dvp_DataView.OnDataChangedHandlerEvent += new SysCommon.UI.DataGridViewForPageCtrl.OnDataChangedHandler(dvp_DataView_OnDataChangedHandlerEvent);
            InitDataGridView();
            InitCombolBox();

            SysCommon.MsgBox.ProecssingMessageBox.EndProecssing();
        }

        #region 导出Excel
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_ExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                //frm.btn_ExportToExcel.Enabled = false;

                SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys =
                    (SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[])frm.dvp_DataView.dg_DataView.DataSource;
                if (etys == null)
                {
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有要导出的数据", false);
                    return;
                }
                if (etys.Length <= 0)
                {
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有要导出的数据", false);
                    return;
                }
                DateTime datetime = System.DateTime.Now;
                string path = Application.StartupPath + @"\Resource\ReportsTemplate\广东省车辆信息统计报表（车队管理）.xls";

                Data.ExcelData.ExportVehicleInfoByCarmember(
                    path, etys, datetime);
                frm.btn_ExportToExcel.Enabled = true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用btn_ExportToExcel_Click出错", ex);
                frm.btn_ExportToExcel.Enabled = true;
            }
        }
        #endregion

        #region 打印
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Print_Click(object sender, EventArgs e)
        {
            try
            {
                //frm.btn_Print.Enabled = false;
                SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys =
                    (SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[])frm.dvp_DataView.dg_DataView.DataSource;
                if (etys == null || etys.Length <= 0)
                {
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有要打印的数据", false);
                    return;
                }
                DateTime datetime = System.DateTime.Now;
                string path = Application.StartupPath + @"\Resource\ReportsTemplate\广东省车辆信息统计报表（车队管理）.xls";

                Data.ExcelData.PrintVehicleInfoByCarmember(
                    path, etys, datetime);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用btn_Print_Click出错", ex);
            }
            frm.btn_Print.Enabled = true;
        }
        #endregion

        #region 开始统计
        /// <summary>
        /// 开始统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Start_Click(object sender, EventArgs e)
        {
            //StatInfo();
            //StatInfo2();
            try
            {
                SysCommon.MsgBox.ProecssingMessageBox.StartProecssing(this.frm, "正在加载数据，请稍候...");
                System.Windows.Forms.Application.DoEvents();
                StatInfoWithCompressForPage();
                SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] m_Etys = (SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[])frm.dvp_DataView.dg_DataView.DataSource;

                SysCommon.MsgBox.ProecssingMessageBox.EndProecssing();
                if (m_Etys == null || m_Etys.Length == 0)
                {
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有找到符合条件的结果", false);
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用btn_Query_Click(object sender, EventArgs e)出错", ex);
            }
        }
        #endregion

        #region 基本信息初始化
        #region 初始化DataGridView
        /// <summary>
        /// 初始化DataGridView
        /// </summary>
        void InitDataGridView()
        {
            try
            {
                SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys =
                    new SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[0];
                if (etys == null) return;
                string[] cn_col = new string[] {"车队名称","车辆类型","车辆用途","车辆数量"};
                string[] en_col = new string[] { "CARMEMBERNAME", "CARTYPENAME", "CARUSENESS", "CARNUMBER"};

                SysCommon.Tools.ShowEntityForDataGridView.SetDefaultDataGrigStyle(frm.dvp_DataView.dg_DataView);
                SysCommon.Tools.ShowEntityForDataGridView.EntityToView(cn_col, en_col, etys, frm.dvp_DataView.dg_DataView);
                frm.dvp_DataView.dg_DataView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用CVSS.Client.CIQS.UI.InitDataGridView出错", ex);
            }
        }
        #endregion
      
        #region 初始化ComboBox
        /// <summary>
        /// 初始化ComboBox
        /// </summary>
        void InitCombolBox()
        {
            try
            {
                //初始化车队
                //InitCarMember(frm.cmb_CarMember);
                InitCarMember2(frm.cmb_CarMember);
                //初始化车辆类型
                GetSystemSettings("VehicleType", frm.cmb_CarType);

                //初始化车辆用途
                InitCarUsess(frm.cmb_CarUsessType);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用CVSS.Client.CIQS.UI.InitDataGridView出错", ex);
            }
        }
        #endregion

        #region 初始化车辆用途信息
        /// <summary>
        /// 初始化车辆用途信息
        /// </summary>
        /// <param name="m_ComboBox"></param>
        void InitCarUsess(System.Windows.Forms.ComboBox m_ComboBox)
        {
            try
            {
                SysCommon.Entity.Vehicle.Car_Ety[] etys = null;
                m_ComboBox.Items.Clear();
                m_ComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl ws = new SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl();
                byte[] zipBuffer = ws.GetAllCarInfo2();
                etys = (SysCommon.Entity.Vehicle.Car_Ety[])CompressionHelper.DeCompressEntity(zipBuffer);
                m_ComboBox.Items.Add("全部");
                foreach (SysCommon.Entity.Vehicle.Car_Ety ety in etys)
                {
                    if (ety.CARUSENESS == null) continue;
                    if (m_ComboBox.Items.Contains(ety.CARUSENESS)) continue;

                    m_ComboBox.Items.Add(ety.CARUSENESS);
                }
                m_ComboBox.Text = "全部";
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用InitCarUsess出错", ex);
            }
        }
        #endregion 

        #region 初始化车辆类型
        /// <summary>
        /// 初始化车辆类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="m_ComboBox"></param>
        void GetSystemSettings(string name, System.Windows.Forms.ComboBox m_ComboBox)
        {
            try
            {
                m_ComboBox.Items.Clear();
                m_ComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

                SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl m_ws = new SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl();
                SysCommon.Entity.CVSS.WS.BaseData.SysSetting_Ety[] etys = m_ws.SearchSysSettingInfo1(name);
                m_ComboBox.Items.Add("全部");

                foreach (SysCommon.Entity.CVSS.WS.BaseData.SysSetting_Ety ety in etys)
                {
                    if (m_ComboBox.Items.Contains(ety.DISPLAYVALUE)) continue;

                    m_ComboBox.Items.Add(ety.DISPLAYVALUE);
                }
                m_ComboBox.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用CVSS.Client.CIQS.UI.GetSystemSettings出错", ex);
            }
        }
        #endregion

        #region 初始化车队列表
        /// <summary>
        /// 初始化车队列表
        /// </summary>
        /// <param name="m_ComboBox"></param>
        void InitCarMember(System.Windows.Forms.ComboBox m_ComboBox)
        {
            m_ComboBox.Items.Clear();
            SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl m_ws = new SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl();
            SysCommon.Entity.CVSS.WS.BaseData.CarMember_Ety[] m_Etys = m_ws.GetAllCarMemberInfo();
            m_ComboBox.Items.Add("全部");
            foreach (SysCommon.Entity.CVSS.WS.BaseData.CarMember_Ety ety in m_Etys)
            {
                if (ety.NAME == null) continue;
                m_ComboBox.Items.Add(ety.NAME);
            }
            m_ComboBox.Text = "全部";
        }
        #endregion

        #region 初始化车队列表(webservice压缩处理)
        /// <summary>
        /// 初始化车队列表(webservice压缩处理)
        /// </summary>
        void InitCarMember2(System.Windows.Forms.ComboBox m_ComboBox)
        {
            m_ComboBox.Items.Clear();
            SysCommon.Entity.Human.CarMember_Ety[] m_Etys=null;
            SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl m_ws = new SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl();
             byte[] zipBuffer= m_ws.GetAllCarMemberInfo2();
             m_Etys = (SysCommon.Entity.Human.CarMember_Ety[])CompressionHelper.DeCompressEntity(zipBuffer);
            m_ComboBox.Items.Add("全部");
            foreach (SysCommon.Entity.Human.CarMember_Ety ety in m_Etys)
            {
                if (ety.NAME == null) continue;
                m_ComboBox.Items.Add(ety.NAME);
            }
            m_ComboBox.Text = "全部";
        }
        #endregion
       #endregion

        #region 统计函数备份
        #region 统计函数
        void StatInfo()
        {
            try
            {
                SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl ws = 
                    new SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl();

                SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety query=
                    new SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety();

                query.CARMEMBERNAME=frm.cmb_CarMember.Text.Trim();
                query.CARTYPE=frm.cmb_CarType.Text.Trim();
                query.CARUSENESS=frm.cmb_CarUsessType.Text.Trim();
                if(query.CARMEMBERNAME=="全部")query.CARMEMBERNAME="";
                if(query.CARTYPE=="全部")query.CARTYPE="";
                if(query.CARUSENESS=="全部")query.CARUSENESS="";
                SysCommon.Entity.CVSS.WS.Stat.View_statcarbycarmember_Ety[] etys = ws.StatCarInfoByCarmember(
                    query);

                if (etys == null)
                {
                    InitDataGridView();
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的统计结果！", false);
                }
                else
                {
                    frm.dvp_DataView.dg_DataView.DataSource = etys;
                    if (etys.Length == 0)
                        SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的查询结果！", false);
                }

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StatInfo()出错", ex);
            }
        }
        #endregion

        #region 统计函数(webservice压缩处理)
        /// <summary>
        /// 统计函数(webservice压缩处理)
        /// </summary>
        void StatInfo2()
        {
            try
            {
                SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl ws =
                    new SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl();

                SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety query =
                    new SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety();
                SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys=null;

                query.CARMEMBERNAME = frm.cmb_CarMember.Text.Trim();
                query.CARTYPE = frm.cmb_CarType.Text.Trim();
                query.CARUSENESS = frm.cmb_CarUsessType.Text.Trim();
                if (query.CARMEMBERNAME == "全部") query.CARMEMBERNAME = "";
                if (query.CARTYPE == "全部") query.CARTYPE = "";
                if (query.CARUSENESS == "全部") query.CARUSENESS = "";
                 byte[] zipBuffer= ws.StatCarInfoByCarmember2(query);
                 etys = (SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[])CompressionHelper.DeCompressEntity(zipBuffer);

                if (etys == null)
                {
                    InitDataGridView();
                    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的统计结果！", false);
                }
                else
                {
                    frm.dvp_DataView.dg_DataView.DataSource = etys;
                    if (etys.Length == 0)
                        SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的查询结果！", false);
                }

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StatInfo2()出错", ex);
            }
        }
        #endregion
        #endregion

        #region 统计函数方法压缩且分页
        /// <summary>
        /// 统计函数方法压缩且分页
        /// </summary>
        void StatInfoWithCompressForPage()
        {
            try
            {
                int pageIndex = 1;
                int pageSize = 30;
                SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl ws =
                    new SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl();

                SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety query =
                    new SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety();
                //SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys = null;

                query.CARMEMBERNAME = frm.cmb_CarMember.Text.Trim();
                query.CARTYPENAME = frm.cmb_CarType.Text.Trim();
                query.CARUSENESS = frm.cmb_CarUsessType.Text.Trim();
                if (query.CARMEMBERNAME == "全部") query.CARMEMBERNAME = "";
                if (query.CARTYPENAME == "全部") query.CARTYPENAME = "";
                if (query.CARUSENESS == "全部") query.CARUSENESS = "";

                byte[] zipBuffer = ws.StatCarInfoByCarmemberWithCompressForPage(query,pageIndex,pageSize);
                object obj = null;
                if (zipBuffer != null)
                {
                    obj = SysCommon.Tools.CompressionHelper.DeCompressEntity(zipBuffer);
                    InitDataGridView();
                }
                SysCommon.Entity.Business.CompressResult_Ety<SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety> result = obj
                    as SysCommon.Entity.Business.CompressResult_Ety<SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety>;

                string[] cn_col = new string[] { "车队名称", "车辆类型", "车辆用途", "车辆数量" };
                string[] en_col = new string[] { "CARMEMBERNAME", "CARTYPENAME", "CARUSENESS", "CARNUMBER" };
                if (result != null)
                {
                    frm.dvp_DataView.Init(result.PageIndex, result.PageCount, result.PageSize, result.Objects, cn_col,en_col );
                }
                else
                {
                    SysCommon.Entity.Business.Stat.View.View_Statcarbyarea_Ety[] etys =
                        new SysCommon.Entity.Business.Stat.View.View_Statcarbyarea_Ety[0];
                    frm.dvp_DataView.Init(0, 0, 0, etys, cn_col ,en_col);
                }

                #region 备份
                //if (etys == null)
                //{
                //    InitDataGridView();
                //    SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的统计结果！", false);
                //}
                //else
                //{
                //    frm.dvp_DataView.dg_DataView.DataSource = etys;
                //    if (etys.Length == 0)
                //        SysCommon.MsgBox.SysMessageBox.ShowInfoMessageBox(frm, "对不起，没有符合条件的查询结果！", false);
                //}
                #endregion

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用StatInfoWithCompressForPage()出错", ex);
            }
        }

        #region  数据改变时的处理函数dvp_DataView_OnDataChangedHandlerEvent
        /// <summary>
        /// 数据改变时的处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">当前页面数据参数</param>
        /// <returns>新数据</returns>
        object dvp_DataView_OnDataChangedHandlerEvent(object sender, SysCommon.UI.DataGridViewForPageCtrl.OnDataChangedHandlerEventArgs e)
        {
            int pageIndex = e.PageIndex;
            int countPerPage = e.PageSize;
            int pageSize = e.PageSize;
            SysCommon.MsgBox.ProecssingMessageBox.StartProecssing(this.frm, "正在加载数据，请稍候...");
            System.Windows.Forms.Application.DoEvents();
            SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl ws =
                     new SysCommon.Business.CVSS.WS.Stat.WS_Stat_BizCtrlImpl();

            SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety query =
                new SysCommon.Entity.CVSS.WS.Stat.CarmemberstauCondition_Ety();
            //SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety[] etys = null;

            query.CARMEMBERNAME = frm.cmb_CarMember.Text.Trim();
            query.CARTYPE = frm.cmb_CarType.Text.Trim();
            query.CARUSENESS = frm.cmb_CarUsessType.Text.Trim();
            if (query.CARMEMBERNAME == "全部") query.CARMEMBERNAME = "";
            if (query.CARTYPE == "全部") query.CARTYPE = "";
            if (query.CARUSENESS == "全部") query.CARUSENESS = "";

            byte[] zipBuffer = ws.StatCarInfoByCarmemberWithCompressForPage(query, pageIndex,pageSize);

            object obj = null;
            if (zipBuffer != null)
            {
                obj = SysCommon.Tools.CompressionHelper.DeCompressEntity(zipBuffer);
                InitDataGridView();
            }
            SysCommon.Entity.Business.CompressResult_Ety<SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety> result = obj
                as SysCommon.Entity.Business.CompressResult_Ety<SysCommon.Entity.Business.Stat.View.View_statcarbycarmember_Ety>;

            if (result != null)
            {
                SysCommon.MsgBox.ProecssingMessageBox.EndProecssing();
                return result.Objects;
            }
            return null;

        }
        #endregion

        #endregion

    }
}
